<?php
/**
 * 漏洞业务类
 */
class core_service_bug extends core_service {
	protected $id;
	protected $marter_table = 'core_model_bug';

	/**
	 * 实例化对象
	 * @param int $id
	 */
	public function __construct($id = 0) {
		parent::__construct();
		$this->id = $id;
	}
	
	/**
	 * 创建
	 */
	function create($data = array()){
		if(!$data['user_id'] or !$data['title'] or !$data['content'] or !$data['cate_id'] or !$data['level'] or !$data['rank']){
			$this->setError(0, "缺少必要参数");
			return false;
		}
		if($this->checkRank($data['level'], $data['rank']) === false){
			return false;
		}
		$cate = new core_model_cate($data['cate_id']);
		if($cate->getData('pid') <= 0){
			$this->setError(0, "分类不存在");
			return false;
		}
		$this->set('user_id', $data['user_id']);
		$this->set('username', $data['username']);
		$this->set('title', $data['title']);
		$this->set('content', $data['content']);
		$this->set('files', $data['files']);
		$this->set('cate_id', $data['cate_id']);
		$this->set('level', $data['level']);
		$this->set('rank', $data['rank']);
		$this->set('create_time', $this->_time);
		$this->set('create_ymd', date('Y-m-d', $this->_time));
		$this->set('ip', Qutil::getIP());
		try{
			$this->_beginTransaction();
			if($this->save() === false){
				throw new Exception("保存漏洞信息出错。");
			}
			$user = new core_service_user($data['user_id']);
			$user->set('bug_num', $user->get('bug_num')+1);
			if($user->save() === false){
				throw new Exception("保存用户信息出错");
			}
			$this->_commit();
		} catch (Exception $e) {
			$this->setError(0,$e->getMessage());
			$this->_rollBack();
			return false;
		}
	}
	
	/**
	 * 修改
	 */
	function edit($data = array()){
		if(!$this->id) return false;
		if($this->get('is_sure') == 1 or $this->get('is_ignore') == 1){
			$this->setError(0, "已经确认或者忽略不能进行此操作。");
			return false;
		}
		if($data['title']) $this->set('title', $data['title']);
		if($data['content']) $this->set('content', $data['content']);
		if($data['files']) $this->set('files', $data['files']);
		if($data['cate_id']) $this->set('cate_id', $data['cate_id']);
		if($data['level']) $this->set('level', $data['level']);
		if($data['rank']) $this->set('rank', $data['rank']);
		if($this->save() === false){
			$this->setError(0,'编辑漏洞出错');
			return false;
		}
		return true;
	}
	
	/**
	 * 忽略
	 */
	function bugIgnore($reason){
		if(!$this->id) return false;
		if($this->get('is_sure') == 1 or $this->get('is_ignore') == 1){
			$this->setError(0, "已经确认或者忽略不能进行此操作。");
			return false;
		}
		$this->set('is_ignore', 1);
		$this->set('ignore_time', $this->_time);
		$this->set('sure_msg', $reason);
		if($this->save() === false){
			$this->setError(0, "忽略出错");
			return false;
		}
		$user = new core_service_user($this->get('user_id'));
		$user->addNotice(2, "您提交的“{$this->get('title')}”漏洞无影响已被忽略。");
		return true;
	}
	
	/**
	 * 确认
	 */
	function bugSure($level, $rank, $reason){
		if(!$this->id) return false;
		if($this->checkRank($level, $rank) === false){
			return false;
		}
		try{
			$this->_beginTransaction();
			$this->setLock(true);
			if($this->get('is_sure') == 1 or $this->get('is_ignore') == 1){
				throw new Exception("已经确认或者忽略不能进行此操作。");
			}
			$this->set('is_sure', 1);
			$this->set('sure_time', $this->_time);
			$this->set('sure_ymd', date('Y-m-d', $this->_time));
			$this->set('sure_level', intval($level));
			$this->set('sure_rank', intval($rank));
			$this->set('sure_msg', $reason);
			if($this->save() === false){
				throw new Exception("确认状态出错");
			}
			$user = new core_service_user($this->get('user_id'));
			$user->set('rank', $user->get('rank')+$rank);
			$user->set('rank_num', $user->get('rank_num')+$rank);
			$user->set('bug_sure_num', $user->get('bug_sure_num')+1);
			if($user->save() === false){
				throw new Exception("增加用户rank失败");
			}
			$this->_commit();
			$user->addNotice(1, "您提交的“{$this->get('title')}”漏洞已确认，您获得了{$rank}".core_lib_constant::UNIT);
			return true;
		} catch (Exception $e) {
			$this->setError(0,$e->getMessage());
			$this->_rollBack();
			return false;
		}
	}
	
	/**
	 * 公开
	 */
	function bugOpen($time = 0){
		if(!$this->id) return false;
		if($this->get('is_open') == 1){
			$this->setError(0, '已经是公开状态');
			return false;
		}
		if($this->get('is_sure') == 1 or $this->get('is_ignore') == 1){
			$time = $time > $this->_time ? $time : $this->_time;
			$this->set('is_open', 1);
			$this->set('open_time', $time);
			$this->save();
			$user = new core_service_user($this->get('user_id'));
			$user->addNotice(3, "您提交的“{$this->get('title')}”漏洞已公开");
			return true;
		}else{
			$this->setError(0, '还没有确认或者忽略，不能公开');
			return false;
		}
	}
	
	/**
	 * 检查level 和rank 是否相符
	 */
	function checkRank($level, $rank){
		switch ($level){
			case 1:
				$max = 5;
				break;
			case 2:
				$max = 10;
				break;
			default:
				$max = 20;
				break;
		}
		if(intval($rank) < 1 or intval($rank) > $max){
			$this->setError(0, "漏洞等级与".core_lib_constant::UNIT."不匹配");
			return false;
		}
		return true;
	}
}